
export default {
  data() {
    return {
      pageNum: 1,
      curPage: 0,
      pageSize: 10,
      datalist: false
    }
  },
  filters: {
    thumbnail: function (url, param) {
      return url.replace(".png", param + ".png").replace(".jpg", param + ".jpg")
    },
    feedbackText: function (feedback, dealStatus) {
      switch (feedback) {
        case "0":
          return dealStatus == 2 ? "待确认" : "处理中"
        case "1":
          // return "未解决"
          return "已办结"
        case "2":
          // return "已解决"
          return "已办结"
      }
    },
    /**
     * 0 => 赞
     */
    changeVote: function (num) {
       return num > 0 ? num  > 9999 ? (num/10000).toFixed(1).replace('.0', '') + 'W' : num : '赞'
    },
    /**
    * 0 => 评论
    */
    changeDis: function (num) {
      return num > 0 ? num  > 9999 ? (num/10000).toFixed(1).replace('.0', '') + 'W' : num : '评论'
    },
  },
  beforeRouteEnter (to, from, next) {
    console.log(from)
    if(!('detailPraising'==from.name ||'detailReport'==from.name || 'subjectList'==from.name)){
      sessionStorage.removeItem('scrollTop-myconv');
      sessionStorage.setItem('isFirstIn',true);
    }else{
      sessionStorage.setItem('isFirstIn',false);
    }
    if('detailPraising'!=from.name){
      sessionStorage.removeItem('readStatus');
      sessionStorage.removeItem('counts');
    }
    if('detailReport'!=from.name){
      sessionStorage.removeItem('feedback');
    }
    next();
  },
  mounted() {
    if(!this.$util.sessionData('isFirstIn')){
      this.init();
    }
  },
  activated(){
    //初始化
    if(this.$util.sessionData('isFirstIn')){
      this.pageNum = 1;
      this.curPage = 0;
      this.pageSize = 10;
      this.datalist = false;
      this.init();
    }
    this.$util.setTitle("我的随手拍");
    //高度定位
    if( this.$util.sessionData('scrollTop-myconv')){
      document.getElementById('mainScroll').scrollTop = this.$util.sessionData('scrollTop-myconv');
    }else if( this.$util.sessionData('scrollTop-myconv') == '0'){
      document.getElementById('mainScroll').scrollTop = 0;
    }

    //同步列表点赞、评论数据
    if(this.$util.sessionData('counts')){
      let counts = JSON.parse(this.$util.sessionData('counts'));
      if(this.datalist[counts.index]){
        this.datalist[counts.index].praiseCount = counts.praiseCount;
        this.datalist[counts.index].commentCount = counts.commentCount;
        this.datalist[counts.index].praised = counts.praised;
      }
      if(this.datalist[counts.index] &&this.datalist[counts.index].auditCode){
        this.datalist[counts.index].auditCode = counts.auditCode; //审核状态
      }
    }
    //问题处理状态
    if(this.$util.sessionData('feedback')){
      let feedback = JSON.parse(this.$util.sessionData('feedback'));
      if(this.datalist[feedback.index]){
        this.datalist[feedback.index].feedback = feedback.feedback+'';
        this.datalist[feedback.index].dealStatus = feedback.dealStatus;
        this.datalist[feedback.index].auditCode = feedback.auditCode;
      }
    }
    //判断小红点是否显示
    if(this.$util.sessionData('readStatus')){
      let statusObj= JSON.parse(this.$util.sessionData('readStatus'));
      if(statusObj.haveSeen){
        this.datalist[statusObj.index].commenting = false;
      }
    }
  },
  beforeRouteLeave(to, from, next) {
    // 导航离开该组件的对应路由时调用
    // 可以访问组件实例 `this`
    // this.$util.droploadUpOnScroll({'ele':'mainScroll',stop:true})
    if(to.name == 'detailPraising'||to.name == 'detailReport'||to.name == 'subjectList'){
      let scrollTop = document.getElementById('mainScroll').scrollTop;
      if(scrollTop){
        this.$util.sessionData('scrollTop-myconv',scrollTop);
      }else{
        this.$util.sessionData('scrollTop-myconv','0');
      }
    }else{
      this.$util.sessionData('scrollTop-myconv','0');
    }
    next();
  },
  methods: {
    init: function () {
      this.$util.setTitle("我的随手拍");
      this.queryPhotoList();
// 加载更多
      this.$util.droploadUpOnScroll({'ele': 'mainScroll','gap': 100}, this.queryPhotoList)
    },
    //查询列表
    queryPhotoList: function () {
      //如果当前页==下一页则不发请求
      if (this.pageNum == this.curPage) { return } else { this.curPage = this.pageNum; }
      let _this = this;
      var params = {
        "pageNum": _this.pageNum,
        "pageSize": _this.pageSize
      };
      this.$http.post("/convphoto/queryPhotoList", params, {loading: false}).then(function (res) {
        var datas = res.data || [];
        for (var i in datas) {
          datas[i].content = datas[i].content.replace(/\n/g, "<br>");
        }
        if (!_this.datalist) { _this.datalist = [] }
        _this.datalist = _this.datalist.concat(datas);
        if (datas.length >= _this.pageSize) {
          //是否有下一页
          _this.pageNum++;
        } else {
          //
          _this.$util.droploadUpOnScroll({ 'ele': 'mainScroll', 'text': ' ', 'gap': 100, 'stop': true })
        }
      })
    },
    goDetail: function (item,index) {
      if(item.commenting){
        let readStatus = {
          index:index,
          haveSeen:false
        };

        this.$util.sessionData('readStatus',JSON.stringify(readStatus));
      }
      //未通过或者待审核
      let goname = "detailPraising"
      if (item.auditCode == 3 && item.praiseCount==null) {
        //发布到举报
        goname = "detailReport"
      } else if (item.auditCode == 3) {
        //发布到大赞北京
        goname = "detailPraising"
      };
      this.$router.push({
        name: goname,
        params: { id: item.id},
        query:{index:index}
      });

    },
    /**
     * 跳转专题详情页
     */
    goSubject(subjectId){
      this.$router.push({'name':'subjectList',params: { 'subjectId': subjectId }})
    },
  },
  components: {

  }
}
